# coding:utf-8
# @Time    : 2018/9/30 6:40
# @Author  : 0x007er
# @File    : bs.py
# @Software: PyCharm
import requests,base64,paramiko,os,traceback
servers = requests.get('http://104.168.159.24:5000/servers').json()
print servers
#执行命令
def SSH_Exec_Command(IP,PASSWORD,COMMAND):
    try:
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        if os.path.exists('privatekeys/'+PASSWORD) == True:
            private_key = paramiko.RSAKey.from_private_key_file('privatekeys/'+PASSWORD,password='admins')
            ssh.connect(hostname=IP, port=22, username='root', pkey=private_key)
        else:
            ssh.connect(hostname=IP, port=22, username='root', password=PASSWORD)
        stdin, stdout, stderr = ssh.exec_command(COMMAND)
        res, err = stdout.read(), stderr.read()
        result = res if res else err
        ssh.close()
        return result.decode()
    except Exception as e:
        traceback.print_exc()
        print('SSH_Exec_Command:',IP,PASSWORD,COMMAND,e)
        return False
for server in servers:
    print server[1],server[2],server[3]